<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
 * 扩展CodeIgniter控制器类
 */

/**
 * 基础控制器
 * 前端后端控制都继承该控制器类
 */
class Base_Controller extends CI_Controller {

    public function __construct() {
        parent::__construct();

        header('Content-Type: text/html; charset=UTF-8');

        //定义公共常量
        define('VERSION', $this->config->item('version'));
        define('DIRECTORY_NAME', $this->router->fetch_directory());
        define('CLASS_NAME', $this->router->fetch_class());
        define('METHOD_NAME', $this->router->fetch_method());
        define('TOKEN', $this->security->get_csrf_hash());
        define('TIMESTAMP', time());
        define('IS_POST', $_SERVER['REQUEST_METHOD'] === 'POST' && count($_POST) ? true : false);

        //加载公共类库
        $this->load->library(array('error', 'message', 'utility'));
        unset($this->utility); //由于工具类都是静态方法，这里unset掉new后的内容释放内存

        log_message('debug', 'Base_Controller Controller Core Class Initialized');
    }
}

/**
 * 前端控制器
 * 前端的所有控制器都需要继承这个类，它不包含验证
 */
class Front_Controller extends Base_Controller {

    /**
     * 当前登陆用户信息
     *
     * @var User_bo $user
     */
    protected $user;

    public function __construct() {
        parent::__construct();

        //开发模式开启调试信息
        if (defined('ENVIRONMENT') && ENVIRONMENT == 'development') $this->output->enable_profiler(true);

        //定义前端常量
        //...

        //设置模板主题
        $this->load->set_theme('front');

        //获取用户登陆信息
        $this->load->service('user/user_auth_service', 'auth');
        $this->user = $this->auth->get_login_user();

        log_message('debug', 'Front_Controller Controller Core Class Initialized');
    }

    /**
     * 显示错误
     *
     * @param string|array $message
     * @param string $referer
     * @param boolean $refresh
     * @return void
     */
    protected function show_error($message = '', $referer = '', $refresh = false) {
        $this->message->set('state', 'fail');
        $this->message->set('referer', $referer);
        $this->message->set('refresh', $refresh);
        $this->message->set('template', 'message_front');
        $this->message->show($message);
    }

    /**
     * 显示消息
     *
     * @param string|array $message
     * @param string $referer
     * @param boolean $refresh
     * @return void
     */
    protected function show_success($message = '', $referer = '', $refresh = false) {
        $this->message->set('state', 'success');
        $this->message->set('referer', $referer);
        $this->message->set('refresh', $refresh);
        $this->message->set('template', 'message_front');
        $this->message->show($message);
    }
}

/**
 * 后端控制器
 * 后端的所有控制器都需要继承这个类，主要包含验证
 */
class Admin_Controller extends Base_Controller {

    protected $menus = array();

    public function __construct() {
        parent::__construct();

        //开发模式开启调试信息
        if (defined('ENVIRONMENT') && ENVIRONMENT == 'development') $this->output->enable_profiler(true);

        //定义后端常量
        //...

        //设置模板主题
        $this->load->set_theme('admin');
        //后台菜单
        $this->load->service('admin_menu/admin_menu_service');
        $this->menus = $this->admin_menu_service->get_menus(); //var_dump($this->menus);
        $this->load->vars(array('menus' => $this->menus)); //使得模板可直接使用

        log_message('debug', 'Admin_Controller Controller Core Class Initialized');
    }

    /**
     * 显示错误
     *
     * @param string|array $message
     * @param string $referer
     * @param boolean $refresh
     * @return void
     */
    protected function show_error($message = '', $referer = '', $refresh = false) {
        $this->message->set('state', 'fail');
        $this->message->set('referer', $referer);
        $this->message->set('refresh', $refresh);
        $this->message->set('template', 'message_admin');
        $this->message->show($message);
    }

    /**
     * 显示消息
     *
     * @param string|array $message
     * @param string $referer
     * @param boolean $refresh
     * @return void
     */
    protected function show_success($message = '', $referer = '', $refresh = false) {
        $this->message->set('state', 'success');
        $this->message->set('referer', $referer);
        $this->message->set('refresh', $refresh);
        $this->message->set('template', 'message_admin');
        $this->message->show($message);
    }
}

/* End of file MY_Controller.php */
/* Location: ./application/core/MY_Controller.php */
